package com.chinasoft.mall.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.util.SystemSetting;
import com.chinasoft.mall.widget.NetImageView;
import com.chinasoft.mall.widget.downloadimg.ImageLoaderWithCache;

public class ShowImageDialog extends Dialog {

	public ShowImageDialog(MainActivity mainActivity, String imageurl) {
		super(mainActivity, R.style.FullHeightDialog);
		init(mainActivity, imageurl);
	}

	private void init(Context context, String imageurl) {
		setContentView(R.layout.bigimage);
		setCanceledOnTouchOutside(true);
		NetImageView netimage = (NetImageView) findViewById(R.id.netimage);
		final ImageButton ib = (ImageButton) findViewById(R.id.close);
		ib.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ShowImageDialog.this.dismiss();
			}
		});
		ImageLoaderWithCache.releaseImage(imageurl);
		netimage.downloadbySize(imageurl, 0, 0, false, false,
				new NetImageView.ImageViewCallback() {
					@Override
					public Drawable beforeDraw(Drawable drawable) {
						return zoomDrawable(drawable, SystemSetting.SCREEN_WIDTH+254,
								SystemSetting.SCREEN_HEIGHT+350);
					}
				});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ShowImageDialog.this.dismiss();
		}
		return true;
	}

	static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		double screenproportion=(float)width/(float)height;
		double imgproportion=(float)w/(float)h;
		Bitmap oldbmp = drawableToBitmap(drawable); // drawable转换成bitmap
		Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
		float scaleWidth = 1;
		float scaleHeight = 1;
		if(screenproportion<imgproportion){
			scaleHeight = ((float) h / height);
			scaleWidth = ((float) h / height); 
		}else{
			scaleWidth = ((float) w / width); // 计算缩放比例
			scaleHeight = ((float) w / width);
		}
		matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
				matrix, true); // 建立新的bitmap，其内容是对原bitmap的缩放后的图
		return new BitmapDrawable(newbmp); // 把bitmap转换成drawable并返回
	}

	static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap
	{
		int width = drawable.getIntrinsicWidth(); // 取drawable的长宽
		int height = drawable.getIntrinsicHeight();
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565; // 取drawable的颜色格式
		Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap
		Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas); // 把drawable内容画到画布中
		return bitmap;
	}

}
